package com.fc.test.api;

import com.fc.test.model.auto.TAbbAixin;
import com.fc.test.service.TAbbAixinService;
import com.fc.test.util.JsonUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/apiAixinController")
@Api(value = "爱心服务")
public class ApiAixinController {

    @Autowired
    private TAbbAixinService aixinService;

    @ResponseBody
    @PostMapping(value="getAixinByPage",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "分页查询爱心服务")
    public String getAixinByPage(
            @ApiParam(value = "页码") @RequestParam(value = "page") int page,
            @ApiParam(value = "1，同城互助；2，同城众筹；3，同城爱心") @RequestParam(value = "type") int type
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbAixin> list = aixinService.getAixinByPage(page,type);
        if(list.size()>0){
            resMap.put("msg","获取爱心服务列表成功");
        }else{
            resMap.put("msg","到底了");
        }
        resMap.put("list",list);
        resMap.put("success",true);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="releaseAixin",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "发布爱心服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userid",value = "发布人id",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "lianxiphone", value = "联系电话", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "lianximember", value = "联系人", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1，同城互助；2，同城众筹；3，同城爱心", dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="title",value = "标题",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "introduction", value = "信息内容", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "images", value = "图片", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "link", value = "链接", dataType = "String",paramType = "query")
    })
    public String releaseAixin(String userid,String lianxiphone,String lianximember,int type,String title,String introduction,String images,String link ){
        Map<String,Object> resMap = new HashMap<>();
        TAbbAixin tAbbAixin = new TAbbAixin();
        tAbbAixin.setUserid(userid);
        tAbbAixin.setLianximember(lianximember);
        tAbbAixin.setLianxiphone(lianxiphone);
        tAbbAixin.setType(type);
        tAbbAixin.setTitle(title);
        tAbbAixin.setIntroduction(introduction);
        tAbbAixin.setImages(images);
        tAbbAixin.setLink(link);

        int flag = aixinService.releaseAixin(tAbbAixin);
        if(flag>0){
            resMap.put("success",true);
            resMap.put("msg","发布成功");
        }else{
            resMap.put("success",false);
            resMap.put("msg","发布失败");
        }
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="getAixinDetail",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "爱心服务详情")
    public String getAixinDetail(
            @ApiParam(value = "爱心服务id") @RequestParam(value = "axid") String axid
    ){
        Map<String,Object> resMap = aixinService.getQuanminDetail(axid);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="getMyAixinList",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "我发布的爱心服务")
    public String getMyAixinList(
            @ApiParam(value = "页码") @RequestParam(value = "page") int page,
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String  userid
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbAixin> list = aixinService.getMyAixinList(page,userid);
        if(list.size()>0){
            resMap.put("msg","获取我发布的爱心服务列表成功");
        }else{
            resMap.put("msg","到底了");
        }
        resMap.put("list",list);
        resMap.put("success",true);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="deleteAixin",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "爱心服务详情")
    public String deleteAixin(
            @ApiParam(value = "爱心服务id") @RequestParam(value = "axid") String axid
    ){
        Map<String,Object> resMap = aixinService.deleteAixin(axid);
        return JsonUtil.jsonMapStr(resMap);
    }
}
